﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using EvaluationSystem.DAO;
using System.Linq.Expressions;

namespace EvaluationSystem.ConfigurationPages
{
    public partial class StaffPage : System.Web.UI.Page
    {
        StaffManager staffManager = new StaffManager();
        const string CommandType = "CommandType";
        const string Id = "Id";
        Window win = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Utility.IsLogin(this.Page))
            {
#if !DEBUG
                Response.Redirect("../Default.aspx");
#endif
            }
            win = new Window
            {
                ID = "EditStaffWindow",
                Title = "编辑员工",
                Width = Unit.Pixel(400),
                Height = Unit.Pixel(600),
                Modal = true,
                Collapsible = true,
                Maximizable = true,
                Hidden = true, 
            };
            win.AutoLoad.ShowMask = true;
            win.AutoLoad.Mode = LoadMode.IFrame;

            
        }

        protected void Store_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.objectDataSource.SelectParameters[Constants.Start].DefaultValue = e.Start.ToString();
            this.objectDataSource.SelectParameters[Constants.Limit].DefaultValue = e.Limit.ToString();
            this.objectDataSource.SelectParameters[Constants.Sort].DefaultValue = e.Sort;
            this.objectDataSource.SelectParameters[Constants.Dir].DefaultValue = e.Dir.ToString();

            this.store.DataBind();  
        }

        protected void ObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            (this.store.Proxy[0] as PageProxy).Total = (int)e.OutputParameters["count"];
        }

        public  List<Users> GetEmployeesFilter(int start, int limit, string sort, string dir, out int count)
        {
            DataTable dt = staffManager.GetAllUsers();
            IEnumerable<DataRow> rows = null;
            if (start >= 0 && limit > 0)
            {
                rows = dt.Select().Skip(start).Take(limit);
            }

            count = staffManager.GetUsersCount(this.Page);
            List<Users> result = new List<Users>(limit);
            foreach (DataRow row in rows)
            {
                result.Add(new Users
                {
                    Id = (int)row["Id"],
                    UserName = (string)row["UserName"],
                    Groups = (string)row["Groups"],
                   // Password = (string)row["Password"],
                    RoleName = (string)row["RoleName"]
                });
            }
            return result;
        }

        [DirectMethod]
        public void DeleteUser(string userId)
        {
            bool isSuccess = staffManager.DeleteUserById(userId);
            if (isSuccess)
            {
                //update the row count so that the paging works.
                staffManager.GetUsersCount(this.Page, true);
                X.MessageBox.Alert(Constants.Noticfy, "删除成功.");
                gpUsers.Reload();
            }
            else
            {
                X.MessageBox.Alert(Constants.Noticfy, "删除异常，请重新尝试或联系开发人员.");
            }
        }

        [DirectMethod]
        public void EditUsers(string userId,string roleName,string groups)
        {
            win.AutoLoad.Url = Page.ResolveUrl(String.Format("StaffEditPage.aspx?{0}={1}&{2}={3}&{4}={5}", StaffConstants.UserId, userId, StaffConstants.RorleName, roleName, StaffConstants.Groups, groups));
            win.Render(this.Form);
            win.Show();
            
        }
    }
}